import Vue from "vue";
var utils = {};

function param(data) {
	let url = "";
	for (var k in data) {
		let value = data[k] !== undefined ? data[k] : "";
		url += "&" + k + "=" + encodeURIComponent(value);
	}
	return url ? url.substring(1) : "";
}

function getStoraged(key) {
	const data = uni.getStorageSync(key);
	if (data) {
		return data;
	} else {
		return "";
	}
};
// 页面跳转
Vue.prototype.$jump = function(path, data) {
	path += (path.indexOf("?") < 0 ? "?" : "&") + param(data);
	uni.navigateTo({
		url: path,
	});
};
Vue.prototype.$redirectTo = function(path, data) {
	path += (path.indexOf("?") < 0 ? "?" : "&") + param(data);
	uni.redirectTo({
		url: path,
	});
};
Vue.prototype.$reLaunch = function(path, data) {
	path += (path.indexOf("?") < 0 ? "?" : "&") + param(data);
	uni.reLaunch({
		url: path,
	});
};
// 返回
Vue.prototype.$back = function (
  obj = {
    delta: 1,
  }
) {
  uni.navigateBack(obj);
};

// 带参跳转
Vue.prototype.$togo = function(url, data) {
	url += (url.indexOf("?") < 0 ? "?" : "&") + param(data);
	uni.navigateTo({
		url,
	});
};


// 获取登录用户
Vue.prototype.$getUserInfo = function() {
	let res = uni.getStorageSync("userInfo");
	if (res) {
		return res;
	} else {
		return {};
	}
};
Vue.prototype.$checkLogin = (success, fail) => {

	if (getStoraged("phone")) {
		if (success) {
			success()
		}
		return true; //已经登录了！！！不再提交数据了！！！或者将数据写入名为"phone"的storage中去！！！！或者使用如下方式写入名为"phone"的storage中去！！！！！或者使用如下方式写入名为"phone"的参数！！！！！或者直接在页面加载时写入名为"phone"的参数！！！！！或者使用如下方式写入名为"phone
	} else {
		if (fail) {
			fail()
		} else {
			uni.showToast({
				title: "当前操作需要登录",
				icon: "none",
				duration: 2000,
				success: () => {
					setTimeout(() => {
						uni.navigateTo({
							url: "/pages2/login",
						});
					}, 2000);
				},
			});
			return false
		}

	}
}